
package com.wyzz.hr.action;

import java.util.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.SysConfigManager;

public class PositionManageAction extends BaseAction {

	public PositionManageAction() {
		dbConfigManager = DatabaseSysConfigManager.getInstance();
	}

	public String positionInfo() {
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		String fetch[] = { Position.PROP_POSITION_EMP_NO,
				Position.PROP_POSITION_PARENT_ID };
		position = posBo.getPosById(positionId, fetch);
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		fetch = (new String[] { PositionBase.PROP_PB_DEPT_ID });
		pb = pbBo.loadPb(position.getPositionPbId().getId(), fetch);
		if (position.getPositionParentId() != null)
			if (position.getPositionParentId().getPositionPbId() != null) {
				fetch = (new String[] { Position.PROP_POSITION_EMP_NO });
				parentPosition = posBo.getPosById(position
						.getPositionParentId().getId(), fetch);
				parentPb = pbBo.getPBById(position.getPositionParentId()
						.getPositionPbId().getId());
			}
		return "success";
	}

	public List getSelectSubList(String id) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		List selectSubList = pbBo.getSelectSubList(id);
		return selectSubList;
	}

	public List savePosition(String positionId, String selectedPbId, Integer num) {
		String auth = DWRUtil.checkAuth("PositionManageAction", "positionInfo");
		if (!"ADM".equals(auth))
			return null;
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		PositionBase pb = pbBo.getPBById(selectedPbId);
		Map dbMap = dbConfigManager.getProperties();
		String exceedConfig = (String) dbMap.get("sys.position.max.exceed");
		if (exceedConfig == null)
			exceedConfig = "0";
		if (exceedConfig.equals("1")) {
			Integer maxEmpConfig = pb.getPbMaxEmployee();
			Integer actualNum = Integer
					.valueOf(posBo.getPosListByPB(pb).size());
			if (maxEmpConfig == null
					|| maxEmpConfig.intValue() - actualNum.intValue() < num
							.intValue())
				return null;
		}
		List newList = new ArrayList();
		for (int i = 0; i < num.intValue(); i++) {
			Position pos = new Position();
			pos.setPositionParentId(new Position(positionId));
			pos.setPositionPbId(pb);
			pos.setPositionTakeNum(Integer.valueOf(1));
			newList.add(pos);
		}

		if (posBo.savePosList(newList))
			return newList;
		else
			return null;
	}

	public String delPosition(String posId) {
		String auth = DWRUtil.checkAuth("PositionManageAction", "positionInfo");
		if (!"ADM".equals(auth))
			return "\u6CA1\u6709\u6743\u9650\uFF01";
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		String msg = posBo.delPosition(posId, false);
		if (msg == null)
			return "SUCC";
		else
			return msg;
	}

	public String changePosSup(String posId, String selectId) {
		String auth = DWRUtil.checkAuth("PositionManageAction", "positionInfo");
		if (!"ADM".equals(auth))
			return "\u6CA1\u6709\u6743\u9650\uFF01";
		Position targetPos = null;
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		if (selectId.equals("rootid"))
			targetPos = posBo.getPosInCharge(posId);
		else
			targetPos = posBo.getPosById(selectId,
					new String[] { Position.PROP_POSITION_PB_ID });
		if (posId.equals(targetPos.getId()))
			return "\u4E0D\u80FD\u79FB\u52A8\u5230\u81EA\u8EAB\uFF01";
		if (posBo.isSupPos(posId, targetPos.getId()))
			return "\u4E0D\u80FD\u79FB\u52A8\u5230\u4E0B\u7EA7\u804C\u4F4D\uFF01";
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		List supPbIds = pbBo.getSupPBsOfPB(targetPos.getId());
		for (Iterator i$ = supPbIds.iterator(); i$.hasNext();) {
			String pbIdInSup = (String) i$.next();
			if (pbIdInSup.equals(posId))
				return "\u4E0D\u80FD\u79FB\u52A8\u5230\u8BE5\u804C\u4F4D\uFF01";
		}

		Position pos = posBo.getPosById(posId,
				new String[] { Position.PROP_POSITION_PB_ID });
		pos.setPositionParentId(new Position(targetPos.getId()));
		posBo.savePos(pos);
		return (new StringBuilder()).append("SUCC,").append(selectId).append(
				",").append(pos.getPositionPbId().getPbInCharge()).toString();
	}

	public String getPositionId() {
		return positionId;
	}

	public void setPositionId(String positionId) {
		this.positionId = positionId;
	}

	public PositionBase getPb() {
		return pb;
	}

	public void setPb(PositionBase pb) {
		this.pb = pb;
	}

	public PositionBase getParentPb() {
		return parentPb;
	}

	public Position getParentPosition() {
		return parentPosition;
	}

	public void setParentPosition(Position parentPosition) {
		this.parentPosition = parentPosition;
	}

	public void setParentPb(PositionBase parentPb) {
		this.parentPb = parentPb;
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public String getPageType() {
		return pageType;
	}

	public void setPageType(String pageType) {
		this.pageType = pageType;
	}

	private static final long serialVersionUID = 1L;
	private String positionId;
	private PositionBase pb;
	private Position parentPosition;
	private PositionBase parentPb;
	private Position position;
	private SysConfigManager dbConfigManager;
	private String pageType;
}
